package com.sunyard.client2.conf;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.sunyard.client2.util.PropertiesUtil;
import com.sunyard.client2.util.StringUtil;

public class ConfModelPanel extends JPanel {
	private JTextField textField_model;
	private JTextField textField_filePartName;
	private JTextField textField_busiDate;
	private JTextField textField_busidateValue;
	private JTextField textField_model_attr1;
	private JTextField textField_model_attr2;
	private JTextField textField_model_attr3;
	private JTextField textField_file_attr1;
	private JTextField textField_file_attr2;
	private JTextField textField_file_attr3;
	private JTextField textField_dir;
	private JTextField textField;

	/**
	 * Create the panel.
	 */
	public ConfModelPanel() {
		setLayout(null);

		JLabel label = new JLabel("批次模型:");
		label.setBounds(29, 59, 89, 15);
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		add(label);

		textField_model = new JTextField();
		textField_model.setBounds(154, 56, 155, 21);
		textField_model.setColumns(10);
		textField_model.setText(ConfUtil.getString("modelCode", ""));
		add(textField_model);

		JLabel label_1 = new JLabel("文件模型:");
		label_1.setBounds(356, 62, 89, 15);
		label_1.setHorizontalAlignment(SwingConstants.RIGHT);
		add(label_1);

		textField_filePartName = new JTextField();
		textField_filePartName.setBounds(481, 59, 155, 21);
		textField_filePartName.setColumns(10);
		textField_filePartName.setText(ConfUtil.getString("filePartName", ""));
		add(textField_filePartName);

		JLabel label_2 = new JLabel("时间字段:");
		label_2.setHorizontalAlignment(SwingConstants.RIGHT);
		label_2.setBounds(29, 104, 89, 15);
		add(label_2);

		textField_busiDate = new JTextField();
		textField_busiDate.setColumns(10);
		textField_busiDate.setBounds(154, 101, 155, 21);
		textField_busiDate.setText(ConfUtil.getString("STARTCOLUMN", ""));
		add(textField_busiDate);

		JLabel label_3 = new JLabel("默认时间:");
		label_3.setHorizontalAlignment(SwingConstants.RIGHT);
		label_3.setBounds(356, 101, 89, 15);
		add(label_3);

		textField_busidateValue = new JTextField();
		textField_busidateValue.setColumns(10);
		textField_busidateValue.setBounds(481, 98, 155, 21);
		textField_busidateValue.setText(ConfUtil.getString("STARTDATE", ""));
		add(textField_busidateValue);

		JLabel label_4 = new JLabel("批次属性:");
		label_4.setHorizontalAlignment(SwingConstants.RIGHT);
		label_4.setBounds(29, 147, 89, 15);
		add(label_4);

		textField_model_attr1 = new JTextField();
		textField_model_attr1.setColumns(10);
		textField_model_attr1.setBounds(154, 144, 155, 21);
		textField_model_attr1.setText(ConfUtil.getString("userAttr1", ""));
		add(textField_model_attr1);

		textField_model_attr2 = new JTextField();
		textField_model_attr2.setColumns(10);
		textField_model_attr2.setBounds(348, 144, 155, 21);
		textField_model_attr2.setText(ConfUtil.getString("userAttr2", ""));
		add(textField_model_attr2);

		textField_model_attr3 = new JTextField();
		textField_model_attr3.setColumns(10);
		textField_model_attr3.setBounds(518, 144, 155, 21);
		textField_model_attr3.setText(ConfUtil.getString("userAttr3", ""));
		add(textField_model_attr3);

		JLabel label_5 = new JLabel("文件属性:");
		label_5.setHorizontalAlignment(SwingConstants.RIGHT);
		label_5.setBounds(29, 198, 89, 15);
		add(label_5);

		textField_file_attr1 = new JTextField();
		textField_file_attr1.setColumns(10);
		textField_file_attr1.setBounds(154, 195, 155, 21);
		textField_file_attr1.setText(ConfUtil.getString("userFileAttr1", ""));
		add(textField_file_attr1);

		textField_file_attr2 = new JTextField();
		textField_file_attr2.setColumns(10);
		textField_file_attr2.setBounds(348, 195, 155, 21);
		textField_file_attr2.setText(ConfUtil.getString("userFileAttr2", ""));
		add(textField_file_attr2);

		textField_file_attr3 = new JTextField();
		textField_file_attr3.setColumns(10);
		textField_file_attr3.setBounds(518, 195, 155, 21);
		textField_file_attr3.setText(ConfUtil.getString("userFileAttr3", ""));
		add(textField_file_attr3);

		JButton button = new JButton("保存");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Properties p = ConfUtil.getProperties();
				p.put("modelCode", textField_model.getText());
				p.put("filePartName", textField_filePartName.getText());
				p.put("STARTCOLUMN", textField_busiDate.getText());
				p.put("STARTDATE", textField_busidateValue.getText());
				p.put("userAttr1", textField_model_attr1.getText());
				p.put("userAttr2", textField_model_attr2.getText());
				p.put("userAttr3", textField_model_attr3.getText());
				p.put("userFileAttr1", textField_file_attr1.getText());
				p.put("userFileAttr2", textField_file_attr2.getText());
				p.put("userFileAttr3", textField_file_attr3.getText());
				p.put("dir", textField_dir.getText());
				p.put("token_value", textField.getText());
				PropertiesUtil.writeToFile(new File(ConfUtil.confFile), p);
				ConfUtil.reload();
				JOptionPane.showMessageDialog(null, "保存成功", "info", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		button.setBounds(312, 343, 93, 23);
		add(button);

		JLabel label_6 = new JLabel("文件目录:");
		label_6.setHorizontalAlignment(SwingConstants.RIGHT);
		label_6.setBounds(29, 246, 89, 15);
		add(label_6);

		textField_dir = new JTextField();
		textField_dir.setColumns(10);
		textField_dir.setBounds(154, 243, 401, 21);
		textField_dir.setText(ConfUtil.getString("dir", ""));
		add(textField_dir);

		JButton btnNewButton = new JButton("选择");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				JFileChooser fc = new JFileChooser();
				if (StringUtil.isNotNull(textField_dir.getText())) {
					fc.setCurrentDirectory(new File(textField_dir.getText()));
				}
				fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int returnVal = fc.showOpenDialog(null);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();
					textField_dir.setText(file.getPath());
				}

			}
		});
		btnNewButton.setBounds(581, 242, 93, 23);
		add(btnNewButton);
		
		JLabel label_7 = new JLabel("静态令牌:");
		label_7.setHorizontalAlignment(SwingConstants.RIGHT);
		label_7.setBounds(29, 290, 89, 15);
		add(label_7);
		
		textField = new JTextField();
		textField.setText(ConfUtil.getString("token_value", ""));
		textField.setColumns(10);
		textField.setBounds(154, 287, 401, 21);
		add(textField);
		this.setVisible(true);
		init();
	}

	private void init() {

	}
}
